################################################################################
#  
# Publication: Persson, M., Schakel, W. and Sundell, A. (2023) "A Man's World?
#	The Policy Representation of Women and Men in a Comparative Perspective."
# Perspectives on Politics.
#
# Description: This script produces the figures presented in the results
# section of the paper and in the appendix. Note that this does not include
# Figure 2 and Appendix Figures A1 and A2, the latter being produced in Stata
# (see "persson_pop_code_02_analysis" do-file).
#
# Sections:
# 1. Figure 1
# 2. Figure 3
# 3. Figure 4
# 4. Figure 5
# 5. Appendix Figure C1
#
# Data: "persson_pop_data_01_main" (see "persson_schakel_sundell_pop_01_prep"
# do-file for - partial - construction)
#
# "persson_pop_data_03_figures", predicted values from the main data set using
# the margins command in Stata.
#
################################################################################

rm(list=ls(all=TRUE))
#Set WD
setwd()

#Packages
library(tidyverse)
library(readxl)
library(haven)
library(viridis)


prepdata <- read_dta("persson_pop_data_01_main.dta")

#############################################################################
# 1. Figure 1
#############################################################################

supportdensity <-  prepdata %>%
  ggplot() +
  geom_density(aes(x=men_change), fill="blue", alpha=0.5) +
  geom_density(aes(x=women_change), fill="red", alpha=0.5, lty=2) +
  theme_minimal() +
  labs(x = "Support for policy change", y="Density") +
  theme(aspect.ratio = 0.67) +
  annotate("curve", x = 0.8, y=1.7, xend=0.77, yend=1.6, arrow=arrow(length=unit(0.2, units="cm"))) +
  annotate("curve", curvature=-0.5, x = 0.2, y=1.6, xend=0.28, yend=1.4, arrow=arrow(length=unit(0.2, units="cm"))) +
  annotate("text", x=0.2, y=1.6, hjust=1, label="Men (solid line)\nprefer less change") +
  annotate("text", x=0.8, y=1.7, hjust=0, label="Women (dashed line)\nprefer more change") +
  scale_x_continuous(breaks=c(0, 0.25, 0.5, 0.75, 1), limits = c(-0.1, 1.15)) +
  theme(plot.background = element_rect(fill="white", colour="white"), aspect.ratio = 0.6667)

ggsave("Figure1.png", width = 6, height=4, dpi=300)


#############################################################################
# 2. Figure 3
#############################################################################

predplot <- read_excel("persson_pop_data_03_figures.xlsx", sheet="fig3")

predplot_line <- predplot %>%
  ggplot(aes(x=x)) +
  geom_line(aes(y=point), size=1.05) +
  geom_line(aes(y=hi), linetype="dashed") +
  geom_line(aes(y=lo), linetype="dashed") +
  geom_linerange(data=filter(prepdata, abs(diff)<=0.1), aes(x=diff), ymin=0.100, ymax=0.105, alpha=0.1) +
  theme_minimal() +
  lims(y=c(0.1, 0.35)) +
  labs(x="Preference gap in support for change\n(positive values: men more positive to change)",
       y="Predicted probability of policy change")

png("Figure3.png", width=6, height=5, units="in", res=300)
predplot_line
dev.off()


#############################################################################
# 3. Figure 4
#############################################################################

ngroups <- 10
tile_cc_data <- prepdata %>%
  filter(!is.na(polchange)) %>%
  mutate(groups_men = floor(men_change*ngroups)/ngroups + 0.5/ngroups,
         groups_women = floor(women_change*ngroups)/ngroups + 0.5/ngroups) %>%
  group_by(groups_men, groups_women) %>%
  summarize(polchange = mean(polchange, na.rm=TRUE),
            n = n())


tile_cc <- tile_cc_data %>%
  filter(n>=10) %>%
  ungroup() %>%
  ggplot(aes(x=groups_women, y=groups_men, fill=polchange, label=round(polchange*100,0))) +
  geom_tile(col="black") +
  geom_abline(slope=1, intercept=0, linetype="dashed", alpha=0.5, col="black") +
  scale_fill_viridis( ) +
  geom_text(col="white") +
  geom_text(aes(label=paste0("(", n, ")")), size=2, nudge_y=-0.02, col="white") +
  #geom_text(data = filter(tile_cc_data, n<10), aes(label=paste0("(", n, ")")), size=2, nudge_y=-0.02, col="#777777") +
  theme_minimal() +
  theme(aspect.ratio = 1, legend.position = "none", panel.grid.minor.x = element_blank(), panel.grid.minor.y=element_blank(),
        plot.background = element_rect(colour="white")) +
  labs(x="Support for change among women", y="Support for change among men") +
  scale_x_continuous(breaks = seq(from=0, to = 1, by=0.1), limit = c(0, 1)) +
  scale_y_continuous(breaks = seq(from=0, to = 1, by=0.1), limit = c(0, 1)) +
  annotate("segment", x=0.62, xend=0.72, y=0.45, yend=0.45, arrow=arrow(length=unit(0.2, units="cm")), col="#777777") +
  annotate("segment", y=0.62, yend=0.72, x=0.45, xend=0.45, arrow=arrow(length=unit(0.2, units="cm")), col="#777777") +
  annotate("segment", y=0.38, yend=0.3, x=0.62, xend=0.7, arrow=arrow(length=unit(0.2, units="cm"))  , col="#777777") +
  annotate("text", label="Down to up: Increase men's\nsupport while holding\nwomen's constant",
           x = 0.45, y=0.73, hjust=0, size=2.2, angle=90) +
  annotate("text", label="Left to right: Increase women's\nsupport while holding\nmen's constant",
           y = 0.45, x=0.73, hjust=0, size=2.2) +
  annotate("text", label="Down right: Compare proposals with\nsimilar average support but\nmore popular among women",
           y = 0.28, x=0.72, hjust=0, size=2.2, angle=-45)

tile_cc
ggsave("Figure4.png", width = 6, height=6, dpi=300)


#############################################################################
# 4. Figure 5
#############################################################################

coefdata_moderation_year_country <- read_xlsx("persson_pop_data_03_figures.xlsx", sheet="year_country")

coefplot_mod <- coefdata_moderation_year_country %>%
  ggplot(aes(x=v1, y=reorder(var, order))) +
  geom_vline(xintercept=0, linetype="dashed") +
  geom_point(size=2) +
  geom_linerange(aes(xmin=l95, xmax=h95, y=reorder(var, order))) +
  theme_minimal() +
  theme() +
  labs(x="Interaction contextual variable * difference in preferences", y=element_blank())

png("Figure5.png", width=6, height=5, units="in", res=300)
coefplot_mod
dev.off()

coefdata_moderation_year <- read_xlsx("persson_pop_data_03_figures.xlsx", sheet="year")
coefdata_moderation_year_issue <- read_xlsx("persson_pop_data_03_figures.xlsx", sheet="year_issue")
coefdata_moderation_year_country_issue <- read_xlsx("persson_pop_data_03_figures.xlsx", sheet="year_country_issue")

multiplier <- 0.7


#############################################################################
# 5. Appendix Figure C1
#############################################################################

coefplot_mod <- coefdata_moderation_year %>%
  ggplot(aes(x=v1, y=reorder(var, order))) +
  geom_vline(xintercept=0, linetype="dashed") +
  geom_point(size=2, alpha=0) +
  geom_point(size=2, aes(y=order+0.3*multiplier), shape=4) +
  geom_linerange(aes(xmin=l95, xmax=h95, y=order+0.3*multiplier)) +
  
  geom_point(data=coefdata_moderation_year_country, size=2, aes(y=order+0.1*multiplier)) +
  geom_linerange(data = coefdata_moderation_year_country, aes(xmin=l95, xmax=h95, y=order+0.1*multiplier)) +
  
  geom_point(data=coefdata_moderation_year_issue, size=2, aes(y=order-0.1*multiplier), shape=1) +
  geom_linerange(data = coefdata_moderation_year_issue, aes(xmin=l95, xmax=h95, y=order-0.1*multiplier)) +
  
  geom_point(data=coefdata_moderation_year_country_issue, size=2, aes(y=order-0.3*multiplier), shape=17) +
  geom_linerange(data = coefdata_moderation_year_country_issue, aes(xmin=l95, xmax=h95, y=order-0.3*multiplier)) +
  
  annotate("point", x=0.3, y=7.5+0.3*multiplier*2, shape=4) +
  annotate("point", x=0.3, y=7.5+0.1*multiplier*2) +
  annotate("point", x=0.3, y=7.5-0.1*multiplier*2, shape=1) +
  annotate("point", x=0.3, y=7.5-0.3*multiplier*2, shape=17) +
  
  annotate("text", x=0.3, y=8.3, hjust=0, size=3, label="Fixed effects") +
  annotate("text", x=0.35, y=7.5+0.3*multiplier*2, hjust=0, size=2.5, label="Year") +
  annotate("text", x=0.35, y=7.5+0.1*multiplier*2, hjust=0, size=2.5, label="Year + country") +
  annotate("text", x=0.35, y=7.5-0.1*multiplier*2, hjust=0, size=2.5, label="Year + issue") +
  annotate("text", x=0.35, y=7.5-0.3*multiplier*2, hjust=0, size=2.5, label="Year + country + issue") +
  
  theme_minimal() +
  theme() +
  labs(x="Interaction contextual variable * difference in preferences", y=element_blank()) +
  lims(x=c(-0.75, 0.75))

png("FigureC1.png", width=6, height=5, units="in", res=300)
coefplot_mod
dev.off()